<script setup lang="ts">
import { computed, watch } from 'vue'
import { useLayoutStore } from '@/stores/layout'
import { useMenuStore } from '@/stores/menu'
import type { RouteMenu } from '@/type/Menu'
import SMenu from '@/components/SMenu.vue'
import logo2 from '@/assets/default-avatar.jpg'
import { useRouter } from 'vue-router'

const router = useRouter()
const menuStore = useMenuStore()
const layoutStore = useLayoutStore()

const routeMenu = computed<RouteMenu[]>(() => menuStore.dynamic)
const selected = computed<string | null>(() => menuStore.currentMenu)

const collapse = computed({
  get: () => layoutStore.side,
  set: (b: boolean) => layoutStore.setSide(b)
})
</script>

<template>
  <div class="s-logo">
    <img :src="logo2" alt="logo" />
    <span v-if="collapse" class="logo-text">SABER</span>
  </div>

  <el-menu
    :default-active="selected"
    v-model="selected"
    class="s-menu"
    :collapse="!collapse"
    background-color="var(--sidebar-bg)"
    text-color="var(--sidebar-text)"
    active-text-color="var(--sidebar-active)"
  >
    <el-menu-item index="home" @click="router.push('/')">
      <el-icon><House /></el-icon>
      <span>首页</span>
    </el-menu-item>
    <template v-for="it in routeMenu" :key="it.menuId" >
      <s-menu v-if="it.visible === 0" :menu="it" />
    </template>

  </el-menu>


</template>

<style scoped lang="scss">
@use '@/style/saber/colors.module' as *;

.s-menu {
  height: calc(100vh - 64px);
  border-right: none;
  transition: all 0.3s ease;
  box-shadow: inset -1px 0 0 rgba(255, 255, 255, 0.1);
  
  :deep(.el-menu-item) {
    border-radius: 0;
    margin: 0;
    transition: all 0.2s ease;
    position: relative;
    border: none;
    height: 48px;
    line-height: 48px;
    font-size: 14px;
    font-weight: 400;
    
    .el-icon {
      font-size: 18px;
      margin-right: 12px;
    }
    
    &:hover {
      background-color: var(--sidebar-hover);
      color: var(--sidebar-text);
      
      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 0;
        height: 100%;
        width: 3px;
        background: var(--sidebar-active);
      }
    }
    
    &.is-active {
      background-color: var(--sidebar-active);
      color: var(--sidebar-text);
      font-weight: 500;
      
      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 0;
        height: 100%;
        width: 3px;
        background: var(--sidebar-active);
      }
      
      .el-icon {
        color: var(--sidebar-text);
      }
    }
  }
  
  :deep(.el-sub-menu) {
    .el-sub-menu__title {
      border-radius: 0;
      margin: 0;
      transition: all 0.2s ease;
      position: relative;
      border: none;
      height: 48px;
      line-height: 48px;
      font-size: 14px;
      font-weight: 500;
      
      .el-icon {
        font-size: 18px;
        margin-right: 12px;
      }
      
      &:hover {
        background-color: var(--sidebar-hover);
        color: var(--sidebar-text);
        
        &::before {
          content: '';
          position: absolute;
          left: 0;
          top: 0;
          height: 100%;
          width: 3px;
          background: var(--sidebar-active);
        }
      }
    }
    
    // 移除父菜单的选中高亮效果，只保留子菜单的选中状态
    // &.is-active {
    //   .el-sub-menu__title {
    //     background-color: var(--sidebar-active);
    //     color: var(--sidebar-text);
    //     font-weight: 500;
    //     
    //     &::before {
    //       content: '';
    //       position: absolute;
    //       left: 0;
    //       top: 0;
    //       height: 100%;
    //       width: 3px;
    //       background: var(--sidebar-active);
    //     }
    //     
    //     .el-icon {
    //       color: var(--sidebar-text);
    //     }
    //   }
    // }
  }
  
  :deep(.el-menu--inline) {
    background-color: transparent;
    padding: 0;
    
    .el-menu-item {
      margin: 0;
      border-radius: 0;
      font-size: 13px;
      height: 40px;
      line-height: 40px;
      padding-left: 56px !important;
      border: none;
      font-weight: 400;
      
      .el-icon {
        font-size: 16px;
        margin-right: 10px;
      }
      
      &:hover {
        background-color: var(--sidebar-hover);
        color: var(--sidebar-text);
        
        &::before {
          content: '';
          position: absolute;
          left: 0;
          top: 0;
          height: 100%;
          width: 3px;
          background: var(--sidebar-active);
        }
      }
      
      &.is-active {
        background-color: var(--sidebar-active);
        color: var(--sidebar-text);
        font-weight: 500;
        
        &::before {
          content: '';
          position: absolute;
          left: 0;
          top: 0;
          height: 100%;
          width: 3px;
          background: var(--sidebar-active);
        }
      }
    }
  }
}

.s-logo {
  height: 64px;
  display: flex;
  padding: 0 16px;
  align-items: center;
  background: var(--logo-bg);
  border-right: none;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  
  img {
    width: 32px;
    height: 32px;
    border-radius: 6px;
    transition: all 0.3s ease;
  }
  
  &:hover {
    cursor: pointer;
    background: var(--logo-bg);
    
    img {
      transform: scale(1.05);
    }
    
    .logo-text {
      color: white;
      text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
    }
  }
}

.logo-text {
  margin-left: 12px;
  font-weight: 700;
  font-size: 20px;
  color: white;
  letter-spacing: 1px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

// 折叠状态下的样式优化
:deep(.el-menu--collapse) {
  .el-menu-item, .el-sub-menu__title {
    justify-content: center;
    padding: 0 !important;
    margin: 0 !important;
    border-radius: 0;
    height: 48px;
    line-height: 48px;
    
    .el-icon {
      margin-right: 0;
      font-size: 20px;
    }
    
    span {
      display: none;
    }
    
    &:hover {
      background-color: var(--sidebar-hover);
      
      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 0;
        height: 100%;
        width: 3px;
        background: var(--sidebar-active);
      }
    }
  }
  
  // 移除折叠状态下父菜单的选中高亮效果
  // .el-sub-menu.is-active .el-sub-menu__title {
  //   background-color: var(--sidebar-active);
  //   
  //   &::before {
  //     content: '';
  //     position: absolute;
  //     left: 0;
  //     top: 0;
  //     height: 100%;
  //     width: 3px;
  //     background: var(--sidebar-active);
  //   }
  // }
}

</style>